var req;
function initAJAX() {
	req = false;
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest;
		} catch(e) {
			req = false;
		}
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	return req;
}

function process() {
	var result = document.getElementById("result");
	if (req.readyState == 4) {
		if (req.status && req.status == 200) {
			result.innerHTML = req.responseText;
		}
	}
}

function getDefinition() {
	initAJAX();
	var form = document.getElementById("theform");
	var query = form.q.value;
	var url = "request.php";
	var data = "act=lookup&q="+encodeURI(query);
	if (req != null) {
		req.onreadystatechange = process;
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(data);
	}
}

function addDefinition() {
	initAJAX();
	var form = document.getElementById("theform");
	var query = form.q.value;
	var formtwo = document.getElementById("theformtwo");
	var url = "request.php";
	var data = "act=add&q="+encodeURI(query)+"&partofspeech="+encodeURI(formtwo.partofspeech.value)+"&definition="+encodeURI(formtwo.definition.value);
	if (req != null) {
		req.onreadystatechange = process;
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(data);
	}
}

function bodyLoad() {
	var form = document.getElementById("theform");
	form.q.focus();
}
